<template>
  <div class="app-container">
    <!--条件搜索表单-->
    <el-row :gutter="20">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-select v-model="searchObj.orderStatus" placeholder="订单状态" clearable @change="getAll()">
            <el-option v-for="item in orderStatusOptions" :key="item.value" :value="item.value" :label="item.text" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input-number v-model="searchObj.priceStart" placeholder="最低金额" controls-position="right" />
        </el-form-item>
        <el-form-item>
          <el-input-number v-model="searchObj.priceEnd" placeholder="最高金额" controls-position="right" />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="searchObj.createTimeBegin"
            type="date"
            placeholder="订单开始日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="searchObj.createTimeEnd"
            type="date"
            placeholder="订单截止日期"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getAll()">查询</el-button>
        <el-button icon="el-icon-refresh" @click="reset()">清空</el-button>
      </el-form>
    </el-row>

    <el-table
      v-loading="loading"
      element-loading-text="正在努力加载数据中..."
      :data="page.list"
      :border="false"
      stripe
      style="width: 100%; margin: 15px 0px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="expand" fixed="left">
        <template v-slot="scope">
          <el-card header="订单详情">
            <el-table :data="scope.row.orderItems">
              <el-table-column prop="product.name" label="商品名称"></el-table-column>
              <el-table-column prop="product.imageUrl" label="商品图片">
                <template v-slot="{ row }">
                  <el-image
                    :src="row.product.imageUrl"
                    :preview-src-list="[row.product.imageUrl]"
                    style="width: 60px; height: 60px"
                  ></el-image>
                </template>
              </el-table-column>
              <el-table-column label="商品规格">
                <template v-slot="{ row }">
                  {{ row.product.price + '元/' + row.product.unit }}
                </template>
              </el-table-column>
              <el-table-column prop="quantity" label="购买数量"></el-table-column>
              <el-table-column prop="price" label="小计">
                <template v-slot="{ row }"> {{ (row.product.price * row.quantity).toFixed(2) }} 元 </template>
              </el-table-column>
            </el-table>
          </el-card>
        </template>
      </el-table-column>
      <el-table-column type="index" fixed="left" width="80" label="序号" />
      <el-table-column prop="orderNo" fixed="left" label="订单编号" min-width="100" sortable show-overflow-tooltip />
      <el-table-column prop="user.username" label="下单用户" min-width="100" show-overflow-tooltip />
      <el-table-column prop="address.name" label="收货人" show-overflow-tooltip />
      <el-table-column prop="address" label="收货地址" min-width="120" show-overflow-tooltip>
        <template v-slot="{ row }">
          {{ row.address.province + row.address.city + row.address.address }}
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="收货人手机号" min-width="130" sortable>
        <template v-slot="{ row }">
          {{ row.address.phone }}
        </template>
      </el-table-column>
      <el-table-column prop="postage" label="运费" min-width="80" sortable>
        <template v-slot="{ row }">
          {{ row.postage + ' 元' }}
        </template>
      </el-table-column>
      <el-table-column prop="totalAmount" label="金额" min-width="80" sortable show-overflow-tooltip>
        <template v-slot="{ row }">
          {{ row.totalAmount + ' 元' }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="下单时间" min-width="100" sortable show-overflow-tooltip />
      <el-table-column prop="orderStatusDesc" label="订单状态" min-width="100" sortable show-overflow-tooltip />
      <el-table-column label="操作" fixed="right" width="150">
        <template v-slot="{ row }">
          <el-link v-if="row.orderStatus == 1" type="primary" @click="handleShipped(row.orderNo)">发货</el-link>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页导航栏 -->
    <div>
      <el-pagination
        :background="true"
        :current-page="pIndex"
        :page-size="pSize"
        :total="page.total"
        :page-sizes="[1, 3, 6, 9, 12, 15, 18]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        @current-change="getAll"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
// 引入api接口定义的js文件
import orderApi from '@/api/order'

export default {
  data() {
    // 定义变量和初始值
    return {
      orderStatusOptions: [
        { value: 0, text: '待支付' },
        { value: 1, text: '待发货' },
        { value: 2, text: '已发货' },
        { value: 3, text: '已完成' },
        { value: 4, text: '已取消' },
        { value: 5, text: '已关闭' }
      ],
      // 查询条件
      searchObj: {},

      // 列表相关
      loading: false, // 加载状态
      pIndex: 1, // 当前页
      pSize: 6, // 每页显示记录数
      page: [] // 列表
    }
  },
  created() {
    this.getAll()
  },
  methods: {
    // 加载列表数据
    getAll(current = 1) {
      this.loading = true
      this.pIndex = current
      orderApi
        .getPage(this.pIndex, this.pSize, this.searchObj)
        .then((response) => {
          this.page = response.data
        })
        .finally(() => {
          this.loading = false
        })
    },

    // 行内筛选
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },

    // 复选框选中
    handleSelectionChange(r) {
      // 得到选中的id
      this.ids = r.map((v) => v.id)
    },

    // 改变页码
    handleSizeChange(pageSize) {
      this.pSize = pageSize
      this.getAll()
    },

    // 重置
    reset() {
      this.searchObj = {}
      this.getAll()
    },

    // 处理发货
    handleShipped(orderNo) {
      orderApi.shipped(orderNo).then((res) => {
        this.getAll()
        this.$message.success('操作成功')
      })
    }
  }
}
</script>

<style></style>
